import requests
import json

url = "https://api.deepseek.com/chat/completions"

payload = json.dumps({
  "messages": [
    # {
    #   "content": "你是一个小学物理老师，尽量长篇大论",
    #   "role": "system" # 系统设置 角色 回复风格
    # },
    {
      "content": "你好",
      "role": "user"
    }
  ],
  "model": "deepseek-chat",
  "n": 1,
  "stream": True
})
headers = {
  'Content-Type': 'application/json',
  'Accept': 'application/json',
  'Authorization': 'Bearer sk-15ee706057da419db34a60b711e78d64'
}

response = requests.request("POST", url, headers=headers, data=payload, stream=True)
print(response.status_code)
print(response.headers['content-type'])

for line in response.iter_lines():
    # print(line)
    if line.startswith(b'data: {'):
        chunk = json.loads(line[6:])['choices'][0]['delta']['content']
        print(chunk, end="", flush=True) 
    elif line == b'data: [DONE]':
        print("[DONE]")        
# print(response.text)
# print(response.json()['choices'][0]['message']['content'])

"""
data: {"id":"22251841-e475-452e-b3d6-4cf202fc7699","object":"chat.completion.chunk","created":1744879375,"model":"deepseek-chat","system_fingerprint":"fp_3d5141a69a_prod0225","choices":[{"index":0,"delta":{"role":"assistant","content":""},"logprobs":null,"finish_reason":null}]}

data: {"id":"22251841-e475-452e-b3d6-4cf202fc7699","object":"chat.completion.chunk","created":1744879375,"model":"deepseek-chat","system_fingerprint":"fp_3d5141a69a_prod0225","choices":[{"index":0,"delta":{"content":"你好"},"logprobs":null,"finish_reason":null}]}

data: {"id":"22251841-e475-452e-b3d6-4cf202fc7699","object":"chat.completion.chunk","created":1744879375,"model":"deepseek-chat","system_fingerprint":"fp_3d5141a69a_prod0225","choices":[{"index":0,"delta":{"content":"！"},"logprobs":null,"finish_reason":null}]}

data: {"id":"22251841-e475-452e-b3d6-4cf202fc7699","object":"chat.completion.chunk","created":1744879375,"model":"deepseek-chat","system_fingerprint":"fp_3d5141a69a_prod0225","choices":[{"index":0,"delta":{"content":"😊"},"logprobs":null,"finish_reason":null}]}

data: {"id":"22251841-e475-452e-b3d6-4cf202fc7699","object":"chat.completion.chunk","created":1744879375,"model":"deepseek-chat","system_fingerprint":"fp_3d5141a69a_prod0225","choices":[{"index":0,"delta":{"content":" "},"logprobs":null,"finish_reason":null}]}

data: {"id":"22251841-e475-452e-b3d6-4cf202fc7699","object":"chat.completion.chunk","created":1744879375,"model":"deepseek-chat","system_fingerprint":"fp_3d5141a69a_prod0225","choices":[{"index":0,"delta":{"content":"很高兴"},"logprobs":null,"finish_reason":null}]}

data: {"id":"22251841-e475-452e-b3d6-4cf202fc7699","object":"chat.completion.chunk","created":1744879375,"model":"deepseek-chat","system_fingerprint":"fp_3d5141a69a_prod0225","choices":[{"index":0,"delta":{"content":"见到"},"logprobs":null,"finish_reason":null}]}

data: {"id":"22251841-e475-452e-b3d6-4cf202fc7699","object":"chat.completion.chunk","created":1744879375,"model":"deepseek-chat","system_fingerprint":"fp_3d5141a69a_prod0225","choices":[{"index":0,"delta":{"content":"你"},"logprobs":null,"finish_reason":null}]}

data: {"id":"22251841-e475-452e-b3d6-4cf202fc7699","object":"chat.completion.chunk","created":1744879375,"model":"deepseek-chat","system_fingerprint":"fp_3d5141a69a_prod0225","choices":[{"index":0,"delta":{"content":"～"},"logprobs":null,"finish_reason":null}]}

data: {"id":"22251841-e475-452e-b3d6-4cf202fc7699","object":"chat.completion.chunk","created":1744879375,"model":"deepseek-chat","system_fingerprint":"fp_3d5141a69a_prod0225","choices":[{"index":0,"delta":{"content":"有什么"},"logprobs":null,"finish_reason":null}]}

data: {"id":"22251841-e475-452e-b3d6-4cf202fc7699","object":"chat.completion.chunk","created":1744879375,"model":"deepseek-chat","system_fingerprint":"fp_3d5141a69a_prod0225","choices":[{"index":0,"delta":{"content":"我可以"},"logprobs":null,"finish_reason":null}]}

data: {"id":"22251841-e475-452e-b3d6-4cf202fc7699","object":"chat.completion.chunk","created":1744879375,"model":"deepseek-chat","system_fingerprint":"fp_3d5141a69a_prod0225","choices":[{"index":0,"delta":{"content":"帮忙"},"logprobs":null,"finish_reason":null}]}

data: {"id":"22251841-e475-452e-b3d6-4cf202fc7699","object":"chat.completion.chunk","created":1744879375,"model":"deepseek-chat","system_fingerprint":"fp_3d5141a69a_prod0225","choices":[{"index":0,"delta":{"content":"的吗"},"logprobs":null,"finish_reason":null}]}

data: {"id":"22251841-e475-452e-b3d6-4cf202fc7699","object":"chat.completion.chunk","created":1744879375,"model":"deepseek-chat","system_fingerprint":"fp_3d5141a69a_prod0225","choices":[{"index":0,"delta":{"content":"？"},"logprobs":null,"finish_reason":null}]}

data: {"id":"22251841-e475-452e-b3d6-4cf202fc7699","object":"chat.completion.chunk","created":1744879375,"model":"deepseek-chat","system_fingerprint":"fp_3d5141a69a_prod0225","choices":[{"index":0,"delta":{"content":""},"logprobs":null,"finish_reason":"stop"}],"usage":{"prompt_tokens":4,"completion_tokens":14,"total_tokens":18,"prompt_tokens_details":{"cached_tokens":0},"prompt_cache_hit_tokens":0,"prompt_cache_miss_tokens":4}}

data: [DONE]

"""